import React, { useState, useEffect } from 'react';
import { BrowserRouter as Router, Route, Routes, Navigate } from 'react-router-dom';
import Login from './components/Login';
import ApplicationForm from './components/ApplicationForm';
import './App.css';

function App() {
  const [token, setToken] = useState(localStorage.getItem('token'));
  const [applicationId, setApplicationId] = useState(null); // This would typically come from a user's application list

  useEffect(() => {
    // In a real application, you would fetch the user's application ID after login
    // For now, we'll use a placeholder or assume it's available.
    // setApplicationId(someFetchedApplicationId);
    setApplicationId(1); // Placeholder for demonstration
  }, [token]);

  return (
    <Router>
      <div className="App">
        <Routes>
          <Route path="/login" element={<Login setToken={setToken} />} />
          <Route
            path="/application/:applicationId"
            element={token ? <ApplicationForm applicationId={applicationId} /> : <Navigate to="/login" replace />}
          />
          <Route path="*" element={<Navigate to={token ? `/application/${applicationId}` : "/login"} replace />} />
        </Routes>
      </div>
    </Router>
  );
}

export default App;


